package com.vdg.fps;

import com.vdg.R;
import com.vdg.fps.config.FPSConfig;
import com.vdg.fps.data.AccountProxy;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;

public class LoginSettingActivity extends Activity {
	private LinearLayout mBtnStartLogin;
	private CheckBox mChbUserProxy;
	private LinearLayout mLnProxySetting;
	private EditText mEdtProxyHost;
	private EditText mEdtProxyPort;
	private CheckBox mChbPrivateProxy;
	private EditText mEdtUserName;
	private EditText mEdtPassword;
	private AccountProxy mAccountProxy;
	public static final String ACCOUNT_PROXY_KEY = "AccountProxy";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_setting_layout);
		initView();
	}

	private void initView() {
		mBtnStartLogin = (LinearLayout) findViewById(R.id.btn_start_login);
		mBtnStartLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginSettingActivity.this,
						LoginActivity.class);
				if (mChbUserProxy.isChecked()) {
					if (!mEdtProxyHost.getText().toString().equals("")
							&& !mEdtProxyPort.getText().toString().equals("")) {
						if (mChbPrivateProxy.isChecked()) {
							if (!mEdtUserName.getText().toString().equals("")) {
								mAccountProxy = new AccountProxy(mEdtProxyHost
										.getText().toString(), mEdtProxyPort
										.getText().toString(), mEdtUserName
										.getText().toString(), mEdtPassword
										.getText().toString());
							}
						} else {
							mAccountProxy = new AccountProxy(mEdtProxyHost
									.getText().toString(), mEdtProxyPort
									.getText().toString());
						}
					}
				}
				if (mAccountProxy != null) {
					intent.putExtra(ACCOUNT_PROXY_KEY, mAccountProxy);
				}
				startActivityForResult(intent,1);
			}
		});
		mLnProxySetting = (LinearLayout) findViewById(R.id.ll_proxy);
		mChbUserProxy = (CheckBox) findViewById(R.id.chb_user_proxy);
		mChbUserProxy.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					mLnProxySetting.setVisibility(View.VISIBLE);
				} else {
					mLnProxySetting.setVisibility(View.INVISIBLE);
				}
			}
		});
		mEdtProxyHost = (EditText) findViewById(R.id.edt_proxy_host);
		mEdtProxyPort = (EditText) findViewById(R.id.edt_proxy_port);
		mChbPrivateProxy = (CheckBox) findViewById(R.id.chb_private_proxy);
		mEdtUserName = (EditText) findViewById(R.id.edt_username);
		mEdtPassword = (EditText) findViewById(R.id.edt_password);
		mChbPrivateProxy
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							mEdtPassword.setVisibility(View.VISIBLE);
							mEdtUserName.setVisibility(View.VISIBLE);
						} else {
							mEdtUserName.setVisibility(View.INVISIBLE);
							mEdtPassword.setVisibility(View.INVISIBLE);
						}
					}
				});
		setUIFont();
	}

	private void setUIFont() {
		Typeface type = Typeface
				.createFromAsset(getAssets(), FPSConfig.UI_FONT);
		mChbUserProxy.setTypeface(type);
		mEdtProxyHost.setTypeface(type);
		mEdtProxyPort.setTypeface(type);
		mChbPrivateProxy.setTypeface(type);
		mEdtUserName.setTypeface(type);
		mEdtPassword.setTypeface(type);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			this.finish();
		}
	}
}
